/**
 * 会员管理路由模块
 */
 const express = require('express')
 const homeuser = express.Router()
 
 // 导入数据库
 const db = require('../../utils/db')
 // 导入加密模块
 const getSha1 = require('../../utils/crypo')

 // oss
 const ossAvatarPath = 'https://admi-yuliner.oss-cn-hangzhou.aliyuncs.com/avatar/'
 
// 指定导航开启
const curNav = 2

 // 获取会员用户列表
 homeuser.get('/userlist', (req, res) => {
   const sql = `SELECT * FROM users ORDER BY id DESC`
   db.query(sql, (err, result) => {
      if (err) throw err
      res.render('HomeUser/homeuserlist', {
         curNav: curNav,
         user: req.session.uname,
         data: result
      })
   })
 })

 // 删除用户
 homeuser.get('/del', (req, res) => {
   const {uid} = req.query
   const sql = `DELETE FROM users WHERE id=${uid}`
   db.query(sql, (err, result) => {
      if (err) throw err
      res.json({status: 200, msg: 'ok'})
   })
 })

 // 查看会员详细信息
 homeuser.get('/userinfo', (req, res) => {
   const {uid} = req.query
   const sql = `SELECT * FROM user_info WHERE u_id=${uid}`
   db.query(sql, (err, result) => {
      if (err) throw err
      if (result[0].pic) {
         result[0].pic = ossAvatarPath + result[0].pic
      }
      res.render('HomeUser/userinfo', {
         curNav: curNav,
         user: req.session.uname,
         data: result
      })
   })
 })

 // 搜搜功能
 homeuser.get('/usersearch', (req, res) => {
   const {pwd} = req.query
   const sql = `SELECT * FROM users WHERE username LIKE '%${pwd}%'`
   db.query(sql, (err, result) => {
      if (err) throw err
      res.json({status: 200, msg: 'ok', data: result})
   })
 })

 // 查看收货地址
 homeuser.get('/useraddress', (req, res) => {
   const {username} = req.query
   const sql = `SELECT * FROM address WHERE username='${username}'`
   db.query(sql, (err, result) => {
      if (err) throw err
      res.render('HomeUser/useraddress', {
         curNav: curNav,
         user: req.session.uname,
         data: result
      })
   })
 })

 // 

 module.exports = homeuser